#!/bin/bash
#默认安装oracle12C



#安装声明
echo "------------------------------------------------------------------------------------"
echo "Oracle12c官方包！！！"
echo "官方下载：https://download.oracle.com/otn/linux/oracle12c/122010/linuxx64_12201_database.zip"
echo "备用下载：http://pan.ffing.cn/d/DSM/Linux_Soft/oracle/12C/linuxx64_12201_database.zip"
echo "百度网盘：https://pan.baidu.com/s/17DSgp6C40s5dWQGi-Q0z7A?pwd=6biz 提取码: 6biz"
echo "------------------------------------------------------------------------------------"
echo -e "\e[33m 	此脚本适配 oracle12c 官方zip安装包,linuxx64_12201_database.zip \e[0m"
echo -e "\e[33m 	官方包名称:linux*database*zip，请保留文件名中的database,以便搜索！\e[0m"
echo -e "\e[33m 	-----------------仅需上传zip包即可,自动安装！-------------------\e[0m"
echo "------------------------------------------------------------------------------------"
echo -e "\e[33m 	此脚本支持默认值自动安装，也可交互配置，懒人就可以直接去喝茶了~\e[0m"
echo "------------------------------------------------------------------------------------"

#sh
SHPATH=`readlink -f $0`
SHDIR=`dirname $SHPATH`



##########################检测oracle安装包是否存在

ZIPWC=`find / -size +3G -name "*database*zip" 2>/dev/null|wc -l`

if [ "$ZIPWC" -eq 0 ] ; then
	echo -e "\e[31m 未找到包含database的zip包！ \e[0m"
	echo "1.自动下载安装"
	echo "按任意键退出！（手动上传后，再次执行本）"

		read -t 20 -p "请20秒内输入(默认1)：" NUM
		if [[  -z  "$NUM"  ||  $NUM -eq 1 ]] ; then
			#安装wget
			wget --version >/dev/null 2>&1
			if [ ! $? -eq 0 ];then yum -y install wget ;fi
			wget http://pan.ffing.cn/d/DSM/Linux_Soft/oracle/12C/linuxx64_12201_database.zip
			ORACLEZIP=$SHDIR/linuxx64_12201_database.zip
			#判断下载状态
			if [ ! $? -eq 0 ];then 
				echo "下载失败，请手动下载，上传Oracle12c！"
				exit
			fi
		else
			exit
		fi
else
	if [ "$ZIPWC" -eq 1 ] ; then 
	while true
	do
	ORACLEZIP=`find / -size +3G -name "*database*zip" 2>/dev/null`
	echo "请核对安装包路径是否正确："
	echo -e "\e[32m 检测到oracle安装包：$ORACLEZIP \e[0m"
	echo "1.确认无误，默认执行安装"
	echo "2.手动输入zip包所在路径"
	echo "3.退出"
	
		read -t 20 -p "请20秒内输入(默认1)：" NUM
		if [ ! -z  "$NUM" ] ; then
		case $NUM in
		1)
		break
		;;

		2)
		while true
		do
			read -p "请输入自定义文件路径(回车，返回上级菜单)：" ORACLEZIP
			if [ -z $ORACLEZIP  ] ;then
			break
			else

				if [ -f "$ORACLEZIP"  ] ; then
        		TEST=`du -m $ORACLEZIP | awk '{print $1}' `
        			if [ "$TEST" -lt 3200 ] ; then
					echo -e "\e[33m 您输入的 $ORACLEZIP 小于3.2G,官方包为3.2G，请重新输入！ \e[0m"
        			else
        			echo "文件正确！"
        			break 2
        			fi
				else
				echo -e "\e[33m 您输入的 $ORACLEZIP 不是文件或者不存在，请确认！ \e[0m"
				fi
			fi
		done
		;;

		3)
		exit
		;;


		*)
			echo -e "\e[33m 输入错误，请重新输入！ \e[0m"
		;;
	    esac
	

		else
		
		break
	    fi
	done

	else
	#多个zip

	echo -e "\e[31m 存在 $ZIPWC 个database的zip安装包，请手动输入zip： \e[0m"
	find / -size +3G -name "*database*zip" -exec ls -lh {} \; 2>/dev/null
	while true
	do
	echo -e "\e[33m ！！！ 请确认路径以/开头  ！！！ \e[0m"
	read -p "请输入文件路径（12c只有1个压缩包！）：" ORACLEZIP
			if [ -f "$ORACLEZIP"  ] ; then
    		TEST=`du -m $ORACLEZIP | awk '{print $1}' `
    			if [ "$TEST" -lt 3200 ] ; then
    			echo -e "\e[33m 您输入的 $ORACLEZIP 小于3.2G,官方包为3.2G，请重新输入！ \e[0m"
    			else
    			echo "您输入的为路径：$ORACLEZIP"
    			break
    			fi
			else
			echo -e "\e[33m 您输入的 $ORACLEZIP 不是文件或者不存在，请确认！ \e[0m"
			fi
	done
	fi
fi
echo "oracle的zip路径为：$ORACLEZIP"






sleep 1


##########################设置swap大于8G
echo
echo
echo "------------------------------------------------------------------------"
while true
do
SETSWAPG=8
echo "配置swap，oracle最小需要150M的swap"
echo "1.自动配置swap至8G（若当前swap大于8G时，将跳过创建）"
echo "2.手动设置swap"
echo "3.退出"

read -t 20 -p "请20秒内输入(默认1)：" NUM
	if [ ! -z  "$NUM" ];then

	case $NUM in
		1)
		break
		;;
		2)
		while true
		do
		read -p "输入swap固定值(单位G,回车返回上级菜单）：" SETSWAPG
		#判断输入值是否为空
		if [ -z "$SETSWAPG" ];then
			SETSWAPG=8
			break
		else
			#判断是否为整数
			TEST=`echo $SETSWAPG |sed 's/[0-9]//g'`
			if [  -n "$TEST" ];then
			echo "输入值错误，请输入整数，请重新输入！"
			else
			    #判断设置的swap值是否大于剩余空间
				DISKFREESWAP=`df -h |grep "/$"|awk '{print $4}'|cut -d'G' -f1`
				if [  "$SETSWAPG" -ge "$DISKFREESWAP" ]; then
				echo "SWAP容量设置错误，超出当前剩余空间！"
				else
				break 2
				fi
			fi
		fi

		done
		;;

		3)
		exit
		;;


		*)
		echo -e "\e[33m 输入错误，请重新输入！ \e[0m"
		;;
	esac
	else
	break
	fi
done
#单位转换：将G转换为M
SETSWAPM=`echo "$SETSWAPG*1024"|bc`

#当前swap
OLDSWAP=`free -m|awk '/Swap/ {print $2}'`
#swap文件名
RANDOM=`date +%N`


#检测swap
if [ "$OLDSWAP" = "0B" ] ;then
	#0B表示不存在swap，则创建swap
	echo  "正在创建swap,至$SETSWAPG G，请等待 ……"
	SWAPFILE="/var/swapfile$RANDOM"
	dd  if=/dev/zero  of=$SWAPFILE  bs=1M  count=$SETSWAPM > /dev/null 2>&1
	#格式化swap
	mkswap  $SWAPFILE 
	#启动swap
	swapon $SWAPFILE
	#swap权限
	chmod -R 0600 $SWAPFILE > /dev/null 2>&1
	#开机挂载swap
	echo  "$SWAPFILE   swap  swap  defaults  0  0" >>  /etc/fstab
else
	#判断当前swap是否小于设定值
	if [ "$OLDSWAP" -lt  "$SETSWAPM" ] ;then
	ADDSWAP=`echo "$SETSWAPM-$OLDSWAP"|bc`
	SWAPFILE="/var/swapfile$RANDOM"
	echo  "正在扩容swap,至$SETSWAPG G，请等待 ……"
	dd  if=/dev/zero  of=$SWAPFILE  bs=1M  count=$ADDSWAP > /dev/null 2>&1
	#格式化swap
	mkswap $SWAPFILE 
	#启动swap
	swapon $SWAPFILE
	#swap权限
	chmod -R 0600 $SWAPFILE > /dev/null 2>&1
	#开机挂载swap
	echo  "$SWAPFILE   swap  swap  defaults  0  0" >>  /etc/fstab
	else
	echo "设定的swap值，比当前swap小，跳过创建swap！"
	fi
fi


NOWSWAP=`free -h|awk '/Swap/ {print $2}'`
echo "当前swap为:$NOWSWAP"












sleep 1
#检测硬盘剩余空间
echo 
echo
echo "------------------------------------------------------------------------"

DISKFREE=`df -m |grep  "/$"|awk '{print $4}'`
if [  "$DISKFREE" -lt 12288 ];then
	echo "硬盘剩余空间不足!!!!!!!!"
	echo "当前剩余空间 $DISKFREE M"
	echo "包解压需4G空间，安装需8G空间"
	echo -e "\e[31m 请确保至少有12G空间，扩容后再执行此脚本！ \e[0m"
	exit
fi







sleep 1
#判断java环境
echo 
echo
echo "------------------------------------------------------------------------"
java -version > /dev/null 2>&1
if [ $? -eq 0 ] ;then
	cat /etc/profile | grep "JAVA_HOME" > /dev/null
	if [ $? -eq 0 ] ;then
		echo "java环境正常"
	else 
		while true
		do
		echo -e "\e[33m 不支持OPENJAVA JVM,全局变量未找到JAVA_HOME,需卸载！ \e[0m"
		echo "1.自动卸载OPENJAVA,此脚本重新安装JAVA"
		echo "2.退出"
		read -t 20 -p "请20秒内输入(默认1)：" NUM
			if [  ! -z "$NUM" ] ;then
				case $NUM in
				1)
				echo "开始卸载OPENJAVA"
				yum -y remove java*
					#再次判断java是否卸载成功
						cat /etc/profile | grep "JAVA_HOME" > /dev/null
						if [ $? -eq 0 ] ;then
						echo "JAVA卸载失败，请手动卸载！"
						exit
						fi

				echo "开始安装JAVA环境"
				curl -o java8.sh https://gitee.com/ffing/onekey/raw/master/java-8_init.sh && bash java8.sh				
				break
				;;

				2)
				exit
				;;

				*)
				echo -e "\e[33m 输入错误，请重新输入！ \e[0m"
				;;
				esac
			else
			echo "开始卸载OPENJAVA"
			yum -y remove java*
						#再次判断java是否卸载成功
						cat /etc/profile | grep "JAVA_HOME" > /dev/null
						if [ $? -eq 0 ] ;then
						echo "JAVA卸载失败，请手动卸载！"
						exit
						fi
			echo "开始安装JAVA环境"
			curl -o java8.sh https://gitee.com/ffing/onekey/raw/master/java-8_init.sh && bash java8.sh	
			break
			fi
		done
	fi

else
echo "开始安装JAVA环境"
curl -o java8.sh https://gitee.com/ffing/onekey/raw/master/java-8_init.sh && bash java8.sh
fi











sleep 1
############################oracle安装用户
echo 
echo
echo "------------------------------------------------------------------------"
while true
do
ORACLEUSER=oracle
echo "配置oracle安装用户名"
echo "1.默认用户名：oracle"
echo "2.自定义oracle安装用户名"
echo "3.退出"
read -t 20 -p "请20秒内输入(默认1)：" NUM
	if [ ! -z "$NUM" ];then
	case $NUM in
		1)
		ORACLEUSER=oracle
		break
		;;

		2)
		while  true
		do
			read -p "请输入oracle安装用户名(回车返回上级菜单)：" ORACLEUSER	
			#判断输入是否为空
			if [ -z $ORACLEUSER ] ;then
				ORACLEUSER=oracle
				#输入值为空，返回上级
				break
			else	
				
				#判断用户名是否纯数字
				if [ "$ORACLEUSER" -gt 0 ] >/dev/null 2>&1 ;then 
					echo "用户名名不能为纯数字!!!"
				else
					cat /etc/passwd | cut -d ':' -f1 | grep ^$ORACLEUSER$ > /dev/null	
					if [ $? -eq 0 ];then
    				echo "系统已存在用户：$ORACLEUSER"
    				echo "1.重新输入用户名"
    				echo "2.依然使用$ORACLEUSER 作为安装用户名"
    				read -p "请选择输入：" NUM
    				case $NUM in
    					1)
						
						;;

						2)
						break 2
						;;

						*)
						echo -e "\e[33m 输入错误，请重新输入！ \e[0m"
						;;
					esac
    				else
    				break 2   				
    				fi
				fi
			fi
		done

		;;

		3)
		exit
		;;


		*)
		echo -e "\e[33m 输入错误，请重新输入！ \e[0m"
		;;
	esac
	else
	break
	fi
done
echo "oracle安装用户名:$ORACLEUSER"








sleep 1
#创建用户、密码
echo 
echo
echo "------------------------------------------------------------------------"
useradd $ORACLEUSER >/dev/null 2>&1
echo "$ORACLEUSER:oracle" | chpasswd
echo "Oracle安装用户名：$ORACLEUSER"
echo "$ORACLEUSER 密码更新为：oracle"

#oracle安装组
#创建组
groupadd oinstall > /dev/null 2>&1
groupadd dba  > /dev/null 2>&1
groupadd oper  > /dev/null 2>&1

#修改组
usermod -g oinstall $ORACLEUSER
usermod -G dba,oper $ORACLEUSER



sleep 1
############################oracle安装目录
echo 
echo
echo "------------------------------------------------------------------------"
#BASE基目录
while true
do
ORACLEBASE=/usr/local/oracle
echo "配置oracle安装路径"
echo "1.默认BASE路径：/usr/local/oracle"
echo "2.自定义oracle安装路径"
echo "3.退出"
read -t 20 -p "请20秒内输入(默认1)：" NUM
	if [ ! -z "$NUM" ];then
	case $NUM in
		1)
		break
		;;

		2)
		while true
		do
		echo -e "\e[33m ！！！安装路径必须输入全路径！！！ \e[0m"
		read -p "请输入oracle安装路径（回车，返回上级菜单）：" ORACLEBASE
		#判断是否为空值
		if [  -z "$ORACLEBASE" ];then
		#空值返回上级菜单
		ORACLEBASE=/usr/local/oracle
		break
		else

				#判断输入值是否以/开头
				HEADNAME=`echo ${ORACLEBASE:0:1}`
				echo $HEADNAME |grep "/" > /dev/null
				if [ $? -eq 0 ];then
					#创建oracle安装目录
					if [  ! -d "$ORACLEBASE" ];then
					#安装目录不存在则创建
					echo "目录不存在，进行创建"
					
					break 2
					else
						while true
						do
						echo "$ORACLEBASE 目录已存在！"
						echo "1.仍然使用 $ORACLEBASE 作为安装目录"
						echo "2.更换安装目录"
						echo "3.退出"
						read -p "请输入（默认1）：" NUM
						if [ ! -z "$NUM" ];then
						case $NUM in
							1)
							break 3
							;;
							2)
							break 2
							;;
							3)
							exit
							;;
							*)
							echo -e "\e[33m 输入错误，请重新输入！ \e[0m"
							;;
						esac

						else
						break 3
						fi
						done


					fi
				else
					echo -e "\e[33m ！！！安装路径必须输入全路径！！！ \e[0m"
					echo -e "\e[33m ！！！  请确认路径以/开头  ！！！ \e[0m"
				fi
		fi
		done
		;;

		3)
		exit
		;;


		*)
		echo -e "\e[33m 输入错误，请重新输入！ \e[0m"
		;;
	esac
	else
	break
	fi
done
mkdir -p $ORACLEBASE
echo "oracle安装路径：$ORACLEBASE"


#判断安装目录是否为空
if [ "$(ls -A $ORACLEBASE)" ]; then
	while true
	do
	echo -e "\e[33m !  安装目录，需保障是空目录，否则安装失败  ！ \e[0m"
	echo -e "\e[33m !  $ORACLEBASE 为非空目录! \e[0m"
	echo "1.以当前时间备份$ORACLEBASE 后，清空目录$ORACLEBASE"
	echo "2.不备份，直接清空目录$ORACLEBASE"
	echo "3.退出"
	read -t 20 -p "请20秒内输入（默认1）：" NUM
	if [ ! -z "$NUM" ] ;then
	case $NUM in
	1)
	BAK=_bak_
	FILEDATE=`date +%F_%T`
	cp -r $ORACLEBASE $ORACLEBASE$BAK$FILEDATE
	rm -rf $ORACLEBASE/*
	break
	;;

	2)
	rm -rf $ORACLEBASE/*
	break
	;;

	3)
	exit
	;;
	*)
	echo -e "\e[33m 输入错误，请重新输入！ \e[0m"
	;;
	esac
	else
	BAK=_bak_
	FILEDATE=`date +%F_%T`
	cp -r $ORACLEBASE $ORACLEBASE$BAK$FILEDATE
	rm -rf $ORACLEBASE/*
	break
	fi
	done
else
echo "$ORACLEBASE 为空目录，符合安装需求。"
fi
echo "oracle安装路径：$ORACLEBASE"


#创建HOME家目录
mkdir -p $ORACLEBASE/dbhome
ORACLEHOME=$ORACLEBASE/dbhome


#创建oraInventory目录
mkdir -p /home/$ORACLEUSER/oraInventory
ORADIR=/home/$ORACLEUSER/oraInventory
#目录权限
chown -R $ORACLEUSER:oinstall $ORACLEBASE
chown -R $ORACLEUSER:oinstall $ORADIR


sleep 1
############################gdbname
echo 
echo
echo "------------------------------------------------------------------------"
echo -e "\e[33m 本脚本不对gdbname/sid进行判断，请确认gdbname/sid \e[0m"

read -t 20 -p "请20秒内输入全局数据库名gdbname（默认orcl）:" GDBNAME
if [  -z "$GDBNAME" ];then
 GDBNAME=orcl
fi
echo "全局数据库名gdbname为 $GDBNAME "
echo
sleep 1


read -t 20 -p "请20秒内输入实例名SID（默认orcl）:" SID
if [  -z "$SID" ];then
 SID=orcl
fi
echo "实例名SID为 $SID "


sleep 1
##########################添加hosts
echo 
echo
echo "------------------------------------------------------------------------"
while true
do
echo "配置主机名"

ORACLEHOSTNAME=$HOSTNAME
echo "1.默认，使用当前主机名 $ORACLEHOSTNAME"
echo "2.自定义主机名"
echo "3.退出"
read -t 20 -p "请20秒内输入主机名(默认1)：" NUM
	if [ -z "$NUM" ] ;then
	break
	else 
		case $NUM in
		1)
		break
		;;

		2)
		while true
		do
		read -p "请输入主机名（回车返回上级菜单）" ORACLEHOSTNAME
			if 	[ -z "$ORACLEHOSTNAME" ];then
				ORACLEHOSTNAME=$HOSTNAME
				break
			else
				break 2
			fi
		done
		;;

		3)
		exit
		;;

		*)
		echo -e "\e[33m 输入错误，请重新输入！ \e[0m"
		;;

		esac
	fi

done
echo "主机名称：$ORACLEHOSTNAME"
hostnamectl set-hostname $ORACLEHOSTNAME
IPLAN=`hostname -I |awk '{print $1}'`
echo "$IPLAN   $ORACLEHOSTNAME" >> /etc/hosts
systemctl restart network












sleep 1
############################配置用户环境变量
echo 
echo
echo "------------------------------------------------------------------------"
echo "配置用户环境变量"
#备份用户环境变量
USERPROFILEBAK="/home/$ORACLEUSER/.bash_profile-BAK"
if [ ! -f "$USERPROFILEBAK" ]; then
	cp /home/$ORACLEUSER/.bash_profile /home/$ORACLEUSER/.bash_profile-BAK
fi


#删除原有ORACLE相关环境变量
sed -i '/ORACLE_BASE/d'  /home/$ORACLEUSER/.bash_profile
sed -i '/ORACLE_HOME/d' /home/$ORACLEUSER/.bash_profile
sed -i '/ORACLE_SID/d' /home/$ORACLEUSER/.bash_profile


#bash_profile追加环境变量
echo "
ORACLE_HOSTNAME=$ORACLEHOSTNAME
#安装路径
ORACLE_BASE=$ORACLEBASE
#数据库路径
ORACLE_HOME=$ORACLEHOME
#实例id
ORACLE_SID=$SID
#字符集
LANG=zh_CN.UTF-8
NLS_LANG='SIMPLIFIED CHINESE_CHINA.AL32UTF8'
#path路径
PATH=$PATH:$ORACLEHOME/bin
export PATH ORACLE_HOSTNAME ORACLE_BASE ORACLE_HOME ORACLE_SID LANG NLS_LANG
" >> /home/$ORACLEUSER/.bash_profile


sleep 1
############################配置全局环境变量
echo 
echo
echo "------------------------------------------------------------------------"
echo "配置全局环境变量"
#备份全局环境变量
if [ ! -f "/etc/profile-BAK"  ] ;then
	cp /etc/profile /etc/profile-BAK 
fi

#判断是否存在用户ulimit环境变量
cat /etc/profile | grep "$ORACLEUSER" > /dev/null

if [ $? -eq 0 ] ;then
	cat /etc/profile | grep "ulimit -p 65536" > /dev/null
	if [ $? -eq 0 ] ;then
	echo "环境变量正常！"
	else
	echo "全局环境变量异常!!!"
	echo "请删除原有oracle安装用户的环境"
	echo "将下面内容追加到/etc/profile"
	echo "############################"
	echo "umask 022"
	echo "if [ $USER = "你的oracle安装用户名" ]"
	echo "then"
	echo "if [ $SHELL = "/bin/ksh" ]"
	echo "then"
	echo "ulimit -p 16384"
	echo "ulimit -p 65536"
	echo "else"
	echo "ulimit -u 16384 -n 65536"
	echo "fi"
	echo "fi"
	echo "############################"
	exit
	fi

else
	echo "
	umask 022
	if [ $USER = "$ORACLEUSER" ]
	then
		if [ $SHELL = "/bin/ksh" ]
 		then
   		ulimit -p 16384
   		ulimit -p 65536
		else
 		ulimit -u 16384 -n 65536
		fi
	fi
	" >> /etc/profile
fi
#环境生效
source /etc/profile




sleep 1
##########################阀值性能限制：/etc/security/limits.conf
echo 
echo
echo "------------------------------------------------------------------------"
echo "配置阀值性能限制：/etc/security/limits.conf"
#备份原有/etc/security/limits.conf
if [ ! -f "/etc/security/limits.conf-BAK"  ] ;then
	cp /etc/security/limits.conf /etc/security/limits.conf-BAK 
fi


#删除原有oracle安装用户的配置信息
sed -i "/${ORACLEUSER}/d"  /etc/security/limits.conf

#追加配置
echo "
$ORACLEUSER soft nproc 2047
$ORACLEUSER hard nproc 16384
$ORACLEUSER soft nofile 1024
$ORACLEUSER hard nofile 65536
$ORACLEUSER soft stack 10240
$ORACLEUSER hard stack 32768
" >> /etc/security/limits.conf





sleep 1
##########################登录认证： /etc/pam.d/login
echo 
echo
echo "------------------------------------------------------------------------"
echo "配置登录认证： /etc/pam.d/login"

#备份原有/etc/pam.d/login
if [ ! -f "/etc/pam.d/login-BAK"  ] ;then
	cp /etc/pam.d/login /etc/pam.d/login-BAK 
fi

cat /etc/pam.d/login | grep "pam_limits.so" > /dev/null
if [ $? -eq 0 ] ;then
	echo "参数已存在！"
else
	echo "session    required     pam_limits.so" >> /etc/pam.d/login
fi




sleep 1
##########################内核参数调整：/etc/sysctl.conf
echo 
echo
echo "------------------------------------------------------------------------"
echo "配置内核参数：/etc/sysctl.conf"
#备份原有/etc/sysctl.conf
if [ ! -f "/etc/sysctl.conf-BAK"  ] ;then
	cp /etc/sysctl.conf /etc/sysctl.conf-BAK 
fi


cat /etc/sysctl.conf | grep "kernel.shmmax" >/dev/null
if [ $? -eq 0 ] ;then
	cat /etc/sysctl.conf | grep "kernel.shmall" >/dev/null
	if [  $? -eq 0 ] ;then
	echo "内核参数已存在！"
	else
	echo "内核参数异常！"
	echo "将下面内容追加到/etc/sysctl.conf"
	echo "###########################################"
	echo "fs.aio-max-nr = 1048576"
	echo "fs.file-max = 6815744"
	echo "kernel.shmall = 2097152"
	echo "kernel.shmmax = 4294967295"
	echo "kernel.shmmni = 4096"
	echo "kernel.sem = 250 32000 100 128 "
	echo "net.ipv4.ip_local_port_range = 9000 65500"
	echo "net.core.rmem_default = 262144"
	echo "net.core.rmem_max = 4194304"
	echo "net.core.wmem_default = 262144"
	echo "net.core.wmem_max = 1048576"
	echo "###########################################"
	exit
	fi
else 
echo "
#异步IO请求数目 推荐值是：1048576 其实它等于 1024*1024 也就是 1024K 个
fs.aio-max-nr = 1048576
#打开的文件句柄的最大数量,防止文件描述符耗尽的问题
fs.file-max = 6815744
#共享内存8G：8G=8*1024*1024=8388608k/4k=2097152
kernel.shmall = 2097152
#物理内存4G：4G=4*1024*1024*1024=4294967296k-1k=4294967295
kernel.shmmax = 4294967295
#共享内存段的最大数量。shmmni缺省值4096
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128  
#可使用的IPv4端口范围
net.ipv4.ip_local_port_range = 9000 65500
#套接字接收缓冲区大小的缺省值
net.core.rmem_default = 262144
#套接字接收缓冲区大小的最大值
net.core.rmem_max = 4194304
#套接字发送缓冲区大小的缺省值
net.core.wmem_default = 262144
#套接字发送缓冲区大小的最大值
net.core.wmem_max = 1048576
"   >> /etc/sysctl.conf
fi
sysctl -p


sleep 1
#######################创建初始化安装文件：db_install.rsp
echo 
echo
echo "------------------------------------------------------------------------"
echo "创建初始化安装响应文件：db_install.rsp"


echo "oracle.install.responseFileVersion=/oracle/install/rspfmt_dbinstall_response_schema_v12.2.0
oracle.install.option=INSTALL_DB_SWONLY
UNIX_GROUP_NAME=oinstall
INVENTORY_LOCATION=$ORADIR
SELECTED_LANGUAGES=en,zh_CN
ORACLE_HOME=$ORACLEHOME
ORACLE_BASE=$ORACLEBASE
oracle.install.db.InstallEdition=EE
oracle.install.db.OSDBA_GROUP=dba
oracle.install.db.OSOPER_GROUP=oinstall
oracle.install.db.OSBACKUPDBA_GROUP=oinstall
oracle.install.db.OSDGDBA_GROUP=oinstall
oracle.install.db.OSKMDBA_GROUP=oinstall
oracle.install.db.OSRACDBA_GROUP=oinstall
#oracle.install.db.isRACOneInstall=false
oracle.install.db.config.starterdb.type=GENERAL_PURPOSE
oracle.install.db.config.starterdb.globalDBName=orcl
oracle.install.db.config.starterdb.SID=orcl
oracle.install.db.config.starterdb.characterSet=AL32UTF8
oracle.install.db.config.starterdb.memoryOption=true
oracle.install.db.config.starterdb.password.ALL=oracle
SECURITY_UPDATES_VIA_MYORACLESUPPORT=true
DECLINE_SECURITY_UPDATES=true
" > /home/$ORACLEUSER/db_install.rsp







########################创建dbca.rsp
echo 
echo
echo "------------------------------------------------------------------------"
echo "创建库响应文件：dbca.rsp"
echo "responseFileVersion=/oracle/assistants/rspfmt_dbca_response_schema_v12.2.0
gdbName=$GDBNAME
sid=$SID
sysPassword=oracle
systemPassword=oracle
pdbAdminPassword=oracle
dbsnmpPassword=oracle
databaseConfigType=SI
createAsContainerDatabase=true
numberOfPDBs=1
pdbName=orclpdb
templateName=$ORACLEHOME/assistants/dbca/templates/General_Purpose.dbc
emExpressPort=5500
omsPort=0
characterSet=AL32UTF8
listeners=LISTENER
memoryPercentage=40
automaticMemoryManagement=false
totalMemory=0
" > /home/$ORACLEUSER/dbca.rsp














##########################关闭selinux
echo 
echo
echo "------------------------------------------------------------------------"
echo "关闭selinux"
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disable/' /etc/selinux/config







##########################防火墙放行端口：1521/5500
echo 
echo
echo "------------------------------------------------------------------------"
echo "添加放行端口：1521、5500"
sed -i '/<\/zone>/i <port protocol="tcp" port="1521"/> ' /etc/firewalld/zones/public.xml
sed -i '/<\/zone>/i <port protocol="tcp" port="5500"/> ' /etc/firewalld/zones/public.xml
firewall-cmd --reload


echo "----------------------------------------------------------------------------------------"
echo -e "\e[33m   配置结束！开始执行安装，安装周期较长请不要中断脚本运行，直至脚本提示安装结束！\e[0m"
echo -e "\e[33m   配置结束！开始执行安装，安装周期较长请不要中断脚本运行，直至脚本提示安装结束！\e[0m"
echo -e "\e[33m   配置结束！开始执行安装，安装周期较长请不要中断脚本运行，直至脚本提示安装结束！\e[0m"
echo -e "\e[33m   ~~~~~~~~~~~~~~~~~~~~~~~~~~喝杯茶，15分钟左右回来吧~~~~~~~~~~~~~~~~~~~~~~~~~\e[0m"
echo "----------------------------------------------------------------------------------------"

sleep 10





##########################安装依赖包
echo 
echo
echo "------------------------------------------------------------------------"
echo "安装依赖包"


yum install binutils -y
yum install compat-libcap1 -y
yum install compat-libstdc++-33 -y
yum install compat-libstdc++-33.i686 -y
yum install gcc-c++ -y
yum install glibc -y
yum install glibc.i686 -y
yum install glibc-devel -y
yum install glibc-devel.i686 -y
yum install ksh -y
yum install libaio -y
yum install libaio.i686 -y
yum install libaio-devel -y
yum install libaio-devel.i686 -y
yum install libX11 -y
yum install libX11.i686 -y
yum install libXau -y
yum install libXau.i686 -y
yum install libXi -y
yum install libXi.i686 -y
yum install libXtst -y
yum install libXtst.i686 -y
yum install libgcc -y
yum install libgcc.i686 -y
yum install libstdc++ -y
yum install libstdc++.i686 -y
yum install libstdc++-devel -y
yum install libstdc++-devel.i686 -y
yum install libxcb -y
yum install libxcb.i686 -y
yum install make -y
yum install nfs-utils -y
yum install net-tools -y
yum install smartmontools -y
yum install sysstat -y
yum install unixODBC -y
yum install unixODBC-devel -y
yum install unzip -y










######################解压安装包
echo 
echo
echo "------------------------------------------------------------------------"
echo "解压安装包"
unzip -o -d /home/$ORACLEUSER $ORACLEZIP



echo "----------------------------------------------------------------------------------------"
echo -e "\e[33m 	开始安装数据库，安装周期较长请不要中断脚本运行，直至脚本提示安装结束！\e[0m"
echo -e "\e[33m 	开始安装数据库，安装周期较长请不要中断脚本运行，直至脚本提示安装结束！\e[0m"
echo -e "\e[33m 	开始安装数据库，安装周期较长请不要中断脚本运行，直至脚本提示安装结束！\e[0m"
echo -e "\e[33m   	~~~~~~~~~~~~~~~~~~~~~~喝杯茶，15分钟左右回来吧~~~~~~~~~~~~~~~~~~~~~~\e[0m"
echo "----------------------------------------------------------------------------------------"



#数据库初始化
#切换oracle
su - $ORACLEUSER << EOF |tee /tmp/db_install_init.log
#忽略参数 -ignorePrereq
/home/$ORACLEUSER/database/runInstaller -silent -responseFile /home/$ORACLEUSER/db_install.rsp 
EOF

#检测初始化状态
while true 
do
tail /tmp/db_install_init.log|grep $ORACLEHOME/root.sh >/dev/null
if [  $? -eq 0 ];then break;fi
sleep 1
done

echo 
echo
echo "------------------------------------------------------------------------"
echo "更新数据库目录权限"
#返回root
bash $ORADIR/orainstRoot.sh
sleep 2
bash $ORACLEHOME/root.sh
sleep 2



echo 
echo
echo "------------------------------------------------------------------------"
echo "启动监听、创建库"
#切换oracle
su - $ORACLEUSER << EOF
#启动监听
$ORACLEHOME/bin/netca -silent -responseFile /home/$ORACLEUSER/database/response/netca.rsp
sleep 2
#创建数据库
$ORACLEHOME/bin/dbca -silent -createDatabase -responseFile /home/$ORACLEUSER/dbca.rsp
sleep 2
EOF




#开机启动oracle
echo 
echo
echo "------------------------------------------------------------------------"
echo "配置oracle开机启动"
sed -i 's/:N/:Y/' /etc/oratab
sed -i 's/ORACLE_HOME_LISTNER=$1/ORACLE_HOME_LISTNER=$ORACLE_HOME/' $ORACLEHOME/bin/dbstart

echo "
su $ORACLEUSER -lc “$ORACLEHOME/bin/lsnrctl start”
su $ORACLEUSER -lc  $ORACLEHOME/bin/dbstart
" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

#登录数据库启动EM#
su - $ORACLEUSER << EOF
$ORACLEHOME/bin/sqlplus sys/oracle as sysdba 
exec dbms_xdb_config.sethttpsport(5500);
exit
EOF

#判断监听是否成功
netstat -ntlp | awk '{print $4}'|grep "^:::1521$" > /dev/null
if [ $? -eq 0 ] ;then
echo "--------------------------------oracle安装完成---------------------------------"
echo "安装用户：$ORACLEUSER				密码：oracle"
echo "主机名称：$ORACLEHOSTNAME			IP：$IPLAN"
echo "-------------------------------ORACLE相关信息如下-------------------------------"
echo "       ORACLE_BASE：$ORACLEBASE"
echo "       ORACLE_HOME：$ORACLEHOME"
echo "INVENTORY_LOCATION：$ORADIR"
echo "           gdbName：$GDBNAME"
echo "         oracleSID：$SID"
echo "           sys密码：oracle"
echo "        system密码：oracle"
echo "           EM 地址：https://$IPLAN:5500/em"
echo 
echo "注：oracle已配置开机启动！"
echo "------------------------------------------------------------------------------"

else
echo "		 安装失败！"
echo "		 默认监听端口：1521启动失败！"
echo "		 请排查端口占用情况！"
echo "		 可删除下面目录，尝试重新安装"
echo "       ORACLE_BASE：$ORACLEBASE"
echo "       ORACLE_HOME：$ORACLEHOME"
echo "INVENTORY_LOCATION：$ORADIR"
echo "		  本地安装用户：$ORACLEUSER"
fi



